//
//  SLOrderListViewController.m
//  SulitLoanDemo
//
//  Created by 虞嘉伟 on 2025/2/20.
//  
  

#import "SLOrderListViewController.h"
#import <YYKit.h>
#import <Masonry.h>
#import <MJRefresh.h>

#import "DashedLineView.h"
@interface SLOrderListTableViewCell : UITableViewCell
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIStackView *stackView;
@property (nonatomic, strong) UIImageView *decorate;
@property (nonatomic, strong) UIImageView *avatar;
@property (nonatomic, strong) UILabel *productNameLabel;
@property (nonatomic, strong) UILabel *statusLabel;
@property (nonatomic, strong) UIView *whitePanel;
@property (nonatomic, strong) UIView *tablePanel;
@property (nonatomic, strong) DashedLineView *devider1;
@property (nonatomic, strong) UILabel *account;
@property (nonatomic, strong) UILabel *money;
@property (nonatomic, strong) DashedLineView *devider2;
@property (nonatomic, strong) UILabel *loanTime;
@property (nonatomic, strong) UILabel *time;
@property (nonatomic, strong) DashedLineView *devider3;
@property (nonatomic, strong) DashedLineView *devider4; //竖线
@property (nonatomic, strong) UILabel *describe;
@property (nonatomic, strong) SLMoreButton *operationButton;

// 订单状态
@property (nonatomic, assign) NSInteger status;
@end
@implementation SLOrderListTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = UIColor.clearColor;
        self.contentView.backgroundColor = UIColor.clearColor;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self.contentView addSubview:self.bgView];
        [self.contentView addSubview:self.stackView];
        UIView *item1 = [UIView new];
        UIView *item2 = [UIView new];
        [self.stackView addArrangedSubview:item1];
        [self.stackView addArrangedSubview:item2];
        
        [item1 addSubview:self.decorate];
        [item1 addSubview:self.avatar];
        [item1 addSubview:self.productNameLabel];
        [item1 addSubview:self.statusLabel];
        [item1 addSubview:self.whitePanel];
        [self.whitePanel addSubview:self.tablePanel];
        [self.tablePanel addSubview:self.devider1];
        [self.tablePanel addSubview:self.account];
        [self.tablePanel addSubview:self.account];
        [self.tablePanel addSubview:self.money];
        [self.tablePanel addSubview:self.devider2];
        [self.tablePanel addSubview:self.loanTime];
        [self.tablePanel addSubview:self.time];
        [self.tablePanel addSubview:self.devider3];
        [self.tablePanel addSubview:self.devider4];
        [self.whitePanel addSubview:self.describe];
        [item2 addSubview:self.operationButton];
        [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.contentView).inset(20);
            make.top.bottom.equalTo(self.contentView);
        }];
        [self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.contentView).inset(20);
            make.top.equalTo(self.contentView).inset(28);
            make.bottom.equalTo(self.contentView);
        }];
        [self.decorate mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(_decorate.superview);
            make.top.equalTo(self.contentView).inset(20);
            //make.height.mas_equalTo(120);
        }];
        [self.avatar mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_avatar.superview).inset(14);
            make.top.equalTo(_avatar.superview).inset(45);
            make.width.height.mas_equalTo(40);
        }];
        [self.productNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.avatar.mas_right).offset(10);
            make.centerY.equalTo(self.avatar);
        }];
        [self.statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_statusLabel.superview).inset(14);
            make.width.mas_equalTo(self.statusLabel.intrinsicContentSize.width+11*2);
            make.height.mas_equalTo(30);
            make.centerY.equalTo(self.avatar);
        }];
        [self.whitePanel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(_whitePanel.superview);
            make.top.equalTo(self.avatar.mas_bottom).offset(7);
            make.bottom.equalTo(_whitePanel.superview);
        }];
        [self.tablePanel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(self.whitePanel);
        }];
        [self.devider1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.tablePanel).inset(13);
            make.top.equalTo(self.tablePanel).inset(25);
            make.height.mas_equalTo(2);
        }];
        [self.account mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.tablePanel).inset(13);
            make.top.equalTo(self.devider1);
            make.height.mas_equalTo(45);
        }];
        [self.money mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.tablePanel).inset(13);
            make.centerY.equalTo(self.account);
            make.height.equalTo(self.account);
        }];
        [self.devider2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.devider1);
            make.top.equalTo(self.account.mas_bottom);
            make.height.mas_equalTo(2);
        }];
        [self.loanTime mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.account);
            make.top.equalTo(self.devider2.mas_bottom);
            make.height.equalTo(self.account);
        }];
        [self.time mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.money);
            make.centerY.equalTo(self.loanTime);
            make.height.equalTo(self.loanTime);
        }];
        [self.devider3 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.devider1);
            make.top.equalTo(self.loanTime.mas_bottom);
            make.height.mas_equalTo(2);
            make.bottom.equalTo(self.tablePanel).inset(25);
        }];
        [self.devider4 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.devider1);
            make.bottom.equalTo(self.devider3);
            make.centerX.equalTo(self.tablePanel);
            make.width.mas_equalTo(2);
        }];
        [self.describe mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.tablePanel.mas_bottom).offset(12);
            make.left.right.equalTo(self.whitePanel).inset(14);
            make.bottom.equalTo(self.whitePanel).inset(12);
        }];
        [self.operationButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(_operationButton.superview).inset(35);
            make.top.equalTo(_operationButton.superview);
            make.height.mas_equalTo(45);
            make.bottom.equalTo(_operationButton.superview);
        }];
    }
    return self;
}

// 1逾期 2还款中 3未申请 4审核中 5已完成
- (void)setStatus:(NSInteger)status {
    if (status == 1) {
        self.statusLabel.backgroundColor = UIColorHex(#FF8F8F);
    } else if (status == 2) {
        self.statusLabel.backgroundColor = UIColorHex(#A08FFF);
    } else if (status == 3) {
        self.statusLabel.backgroundColor = UIColorHex(#8FAFFF);
    } else if (status == 4) {
        self.statusLabel.backgroundColor = UIColorHex(#FEBB7D);
    } else if (status == 5) {
        self.statusLabel.backgroundColor = UIColorHex(#98D89F);
    }
}

- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [UIView new];
    }
    return _bgView;
}
- (UIStackView *)stackView {
    if (!_stackView) {
        _stackView = [UIStackView new];
        _stackView.spacing = 20;
        _stackView.axis = UILayoutConstraintAxisVertical;
    }
    return _stackView;
}
- (UIImageView *)decorate {
    if (!_decorate) {
        _decorate = [UIImageView new];
        _decorate.image = [UIImage imageNamed:@"icon_order_bubble"];
        _decorate.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _decorate;
}
- (UIImageView *)avatar {
    if (!_avatar) {
        _avatar = [UIImageView new];
        _avatar.backgroundColor = UIColorHex(#F5F5F5);
        _avatar.contentMode = UIViewContentModeScaleAspectFit;
        _avatar.layer.cornerRadius = 20;
    }
    return _avatar;
}
- (UILabel *)productNameLabel {
    if (!_productNameLabel) {
        _productNameLabel = [UILabel new];
        _productNameLabel.font = aspectDefaultFont(14);
        _productNameLabel.textColor = UIColor.whiteColor;
    }
    return _productNameLabel;
}
- (UILabel *)statusLabel {
    if (!_statusLabel) {
        _statusLabel = [UILabel new];
        _statusLabel.font = aspectDefaultFont(14);
        _statusLabel.textAlignment = NSTextAlignmentCenter;
        _statusLabel.text = @"In Arrears";
        _statusLabel.textColor = UIColor.whiteColor;
        _statusLabel.backgroundColor = UIColorHex(#A08FFF);
        _statusLabel.clipsToBounds = YES;
        _statusLabel.layer.cornerRadius = 15;
    }
    return _statusLabel;
}
- (UIView *)whitePanel {
    if (!_whitePanel) {
        _whitePanel = [UIView new];
        _whitePanel.backgroundColor = UIColor.whiteColor;
        _whitePanel.layer.cornerRadius = 20;
        _whitePanel.layer.borderColor = UIColorHex(#1B1B1B).CGColor;
        _whitePanel.layer.borderWidth = 1.5;
    }
    return _whitePanel;
}
- (UIView *)tablePanel {
    if (!_tablePanel) {
        _tablePanel = [UIView new];
        _tablePanel.layer.cornerRadius = 20;
        _tablePanel.layer.borderColor = UIColorHex(#1B1B1B).CGColor;
        _tablePanel.layer.borderWidth = 1.5;
    }
    return _tablePanel;
}

- (DashedLineView *)devider1 {
    if (!_devider1) {
        _devider1 = [DashedLineView new];
    }
    return _devider1;
}
- (UILabel *)account {
    if (!_account) {
        _account = [UILabel new];
        _account.font = aspectDefaultFont(14);
        _account.text = @"In Arrears";
    }
    return _account;
}
- (UILabel *)money {
    if (!_money) {
        _money = [UILabel new];
        _money.font = aspectDefaultFont(14);
        _money.text = @"80,000₱";
        _money.textAlignment = NSTextAlignmentRight;
    }
    return _money;
}

- (DashedLineView *)devider2 {
    if (!_devider2) {
        _devider2 = [DashedLineView new];
    }
    return _devider2;
}
- (UILabel *)loanTime {
    if (!_loanTime) {
        _loanTime = [UILabel new];
        _loanTime.font = aspectDefaultFont(14);
        _loanTime.text = @"Loan Time";
    }
    return _loanTime;
}
- (UILabel *)time {
    if (!_time) {
        _time = [UILabel new];
        _time.font = aspectDefaultFont(14);
        _time.textAlignment = NSTextAlignmentRight;
        _time.text = @"12/20/2024";
    }
    return _time;
}
- (DashedLineView *)devider3 {
    if (!_devider3) {
        _devider3 = [DashedLineView new];
    }
    return _devider3;
}
- (DashedLineView *)devider4 {
    if (!_devider4) {
        _devider4 = [DashedLineView new];
        _devider4.direction = DLDirectionVertical;
    }
    return _devider4;
}
- (UILabel *)describe {
    if (!_describe) {
        _describe = [UILabel new];
        _describe.font = aspectDefaultFont(14);
        _describe.numberOfLines = 0;
        _describe.text = @"三家分晋阿萨法简单介绍拉法基阿发三家分晋阿萨法简单介绍拉法基阿发三家分晋阿萨法简单介绍拉法基阿发";
    }
    return _describe;
}
- (SLMoreButton *)operationButton {
    if (!_operationButton) {
        _operationButton = [SLMoreButton new];
        _operationButton.titleLabel.text = @"";
        _operationButton.titleLabel.textColor = UIColorHex(#1B1B1B);
        _operationButton.imageView.image = [UIImage imageNamed:@"btn_more_white"];
        _operationButton.backgroundColor = UIColorHex(#FDC5E0);
        _operationButton.layer.cornerRadius = 20;
        _operationButton.layer.borderColor = UIColorHex(#1B1B1B).CGColor;
        _operationButton.layer.borderWidth = 1.5;
    }
    return _operationButton;
}
@end




@interface SLOrderListViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UIView *emptyView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *data;
@property (nonatomic, assign) BOOL isRequest;
@end

@implementation SLOrderListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor = UIColorHex(#F5F5F5);
    
    [self.view addSubview:self.emptyView];
    [self.emptyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view).inset(0);
        make.top.equalTo(self.view).inset(0);
        make.bottom.equalTo(self.view).inset(0);
    }];
    
    @weakify(self);
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self);
        [self requestDataWithCompletion:^(NSArray<NSDictionary *> *arr) {
            @strongify(self);
            [self.tableView.mj_header endRefreshing];
            self.data = arr;
            [self.tableView reloadData];
            self.tableView.hidden = (arr.count == 0);
        }];
    }];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if (self.isRequest) {
        return;
    }
    @weakify(self);
    [self requestDataWithCompletion:^(NSArray<NSDictionary *> *arr) {
        @strongify(self);
        self.data = arr;
        [self.tableView reloadData];
        self.tableView.hidden = (arr.count == 0);
    }];
}

- (void)requestDataWithCompletion:(void(^)(NSArray<NSDictionary *> *arr))completion {
    self.isRequest = YES;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [OrderListRequestProvider requestOrderListWithStatus:self.status success:^(NSArray<NSDictionary *> *arr) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        self.isRequest = NO;
        !completion ?: completion(arr);
    } failure:^(YTKRequest * _Nonnull request) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [ResponseToast showToastWithRequest:request];
        self.isRequest = NO;
        !completion ?: completion(nil);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SLOrderListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SLOrderListTableViewCell" forIndexPath:indexPath];
    NSDictionary *dict = self.data[indexPath.row];
    if ([dict isKindOfClass:NSDictionary.class]) {
        NSDictionary *studies = dict[@"studies"];
        [cell.avatar sl_setImageWithURL:studies[@"nice"]]; //logo链接
        cell.productNameLabel.text = studies[@"chair"]; //产品名
        cell.account.text = studies[@"custom"]; //金额描述
        cell.money.text = studies[@"consolatory"]; //金额
        cell.loanTime.text = studies[@"friendly"]; //日期文案
        cell.time.text = studies[@"strengthened"]; //日期
        cell.statusLabel.text = studies[@"acting"]; //右上角订单状态文案
        cell.status = [studies[@"humor"] intValue]; //类型 1逾期 2还款中 3未申请 4审核中 5已完成  此字段用户判断背景色等
        
        cell.describe.text = studies[@"nature"]; //订单描述文案
        
        NSString *counsel = studies[@"counsel"];
        cell.operationButton.hidden = (counsel.length==0);
        cell.operationButton.titleLabel.text = counsel;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dict = self.data[indexPath.row];
    if (![dict isKindOfClass:NSDictionary.class]) {
        return;
    }
    NSDictionary *studies = dict[@"studies"];
    //NSString *orderId = studies[@"mile"]; //订单ID
    NSString *thereby = studies[@"thereby"]; //item 跳转地址
    if (thereby.length > 0) {
        [SLRoutePush route:thereby parameter:nil];
    }
}

// 点击事件
- (void)backHomePageButtonAction:(UIButton *)button {
    [self.navigationController popToRootViewControllerAnimated:NO];
    UITabBarController *tabbarController = (UITabBarController *)UIApplication.sharedApplication.keyWindow.rootViewController;
    tabbarController.selectedIndex = 0;
}

- (UIView *)emptyView {
    if (!_emptyView) {
        _emptyView = [UIView new];
        _emptyView.backgroundColor = UIColorHex(#F5F5F5);
        UIImageView *icon = [UIImageView new];
        icon.image = [UIImage imageNamed:@"icon_order_empty"];
        icon.contentMode = UIViewContentModeScaleAspectFit;
        [_emptyView addSubview:icon];
        [icon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(icon.superview);
            make.top.equalTo(icon.superview).inset(aspectVerticalValue(95));
            make.width.mas_equalTo(aspectValue(258));
            make.height.mas_equalTo(aspectValue(219));
        }];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"Back To Home Page" forState:UIControlStateNormal];
        [button setTitleColor:UIColorHex(#1B1B1B) forState:UIControlStateNormal];
        button.titleLabel.font = aspectDefaultFont(16);
        button.backgroundColor = UIColorHex(#FFADD5);
        button.layer.cornerRadius = 20;
        button.layer.borderColor = UIColorHex(#1B1B1B).CGColor;
        button.layer.borderWidth = 1.5;
        [button addTarget:self action:@selector(backHomePageButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_emptyView addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(icon).inset(40);
            make.top.equalTo(icon.mas_bottom).offset(8);
            make.height.mas_equalTo(40);
        }];
    }
    return _emptyView;
}
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.backgroundColor = UIColorHex(#F5F5F5);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:SLOrderListTableViewCell.class forCellReuseIdentifier:@"SLOrderListTableViewCell"];
    }
    return _tableView;
}

@end
